<template>
  <div>
    <a-button type="primary" @click="visible = !visible">一键生成</a-button>

    <a-modal footer title="自动生成代码" v-model:open="visible" get-container="#GenColumn" centered>
      <a-form @finish="gen" :model="genConfig" :wrapper-col="{ span: 18 }" :label-col="{ span: 6 }">
        <a-card>
          <a-form-item
            label="前端路径"
            name="frontendPath"
            required
            v-if="showFrontendList.includes(genConfig.type)"
          >
            <a-input
              v-model:value="genConfig.frontendPath"
              allow-clear
              placeholder="请输入前端路径"
            >
              <template #addonAfter>
                <div class="flex gap-4">
                  <Path v-model="genConfig.frontendPath"></Path>
                </div>
              </template>
            </a-input>
            <div class="mt-4 text-gray-500 text-[12px]">
              把文件生成的地址，定位到前端package.json目录下，需要绝对路径
            </div>
          </a-form-item>

          <a-form-item
            label="后端路径"
            v-if="showBackendList.includes(genConfig.type)"
            name="backendPath"
            required
          >
            <a-input v-model:value="genConfig.backendPath" allow-clear placeholder="请输入后端路径">
              <template #addonAfter>
                <div class="flex gap-4">
                  <Path v-model="genConfig.backendPath"></Path>
                </div>
              </template>
            </a-input>
            <div class="mt-4 text-gray-500 text-[12px]">
              把文件生成的地址，定位到后端子模块目录下，如ruoyi-ax，需要绝对路径
            </div>
          </a-form-item>

          <a-form-item label="选择类型" name="type" required>
            <a-radio-group :options="codeMenus" v-model:value="genConfig.type"></a-radio-group>
          </a-form-item>
          <a-form-item label="导入菜单" name="runSql">
            <a-switch v-model:checked="genConfig.runSql"></a-switch>
            <div class="text-[12px] text-gray-600 mt-4]">将会生成菜单，如果已经导入，请勿勾选</div>
          </a-form-item>
          <a-form-item label="强制覆盖" name="forceOverride">
            <a-switch v-model:checked="genConfig.forceOverride"></a-switch>
            <div class="text-[12px] text-gray-600 mt-4]">
              开启后：如果之前有生成过的文件，将会直接覆盖。
            </div>
          </a-form-item>
          <a-row>
            <a-col :span="18" :offset="6">
              <a-button type="primary" html-type="submit" :loading="loading">保存</a-button>
              <a-tooltip title="填充配置">
                <a-button class="ml-8" @click="fillHistory">一键填写</a-button>
              </a-tooltip>
            </a-col>
          </a-row>
        </a-card>
      </a-form>
    </a-modal>
  </div>
</template>

<script setup lang="ts">
import { GenWebType, genCodeFile } from '@/api/modules/tool/gen/gen';
import useSystemStore from '@/store/system';
import { response } from '@/utils/table/table';
import Path from '@/views/selector/path/Path.vue';
import { Modal } from 'ant-design-vue';
import { codeMenus } from '../../../../data/options';
import { codeFormData } from '../../data/data';

/* 显示前端列表的类型 */
const showFrontendList = [
  GenWebType.PC,
  GenWebType.MOBILE,
  GenWebType.MOBILE_JAVA,
  GenWebType.PC_JAVA,
  GenWebType.API,
];
/* 显示后端类型的列表 */
const showBackendList = [GenWebType.JAVA, GenWebType.MOBILE_JAVA, GenWebType.PC_JAVA];
const emit = defineEmits(['submit']);

defineProps<{
  loading: boolean;
}>();
const visible = ref(false);

const genConfig = ref({
  frontendPath: ``,
  backendPath: ``,
  runSql: false,
  tableName: '',
  type: GenWebType.PC_JAVA,
  forceOverride: false,
});

const gen = async () => {
  Modal.confirm({
    content: '这将可能会对系统文件造成影响，是否继续？',
    title: '警告',
    centered: true,
    async onOk() {
      const { tableName } = codeFormData.value.info;
      genConfig.value.tableName = tableName;
      await response(genCodeFile, genConfig.value);
      visible.value = false;
    },
  });
};
/**
 * @description: 填充历史记录
 */
const fillHistory = () => {
  const store = useSystemStore();
  genConfig.value.backendPath = store.$state.gen.backendRoot ?? genConfig.value.backendPath;
  genConfig.value.frontendPath = store.$state.gen.frontendRoot ?? genConfig.value.frontendPath;
};
watch(visible, v => {
  if (v) emit('submit');
});
</script>

<style lang="scss" scoped></style>
